<?php
include("../includes/DBFunctions.php");
include("../includes/getCredentials.php");

function regionReportFunctor($rid) {
	$db = new DBFunctions;
	$gc = new GetCredentials;
	$uid = $gc->getUserID();
	$db->connectDB();
	$query = "call region_PlantReport(".$rid.",".$uid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (region_PlantReport)');");
	}
	$toPrint = "<div><h3>Plants</h3><ul>";
	if (mysqli_num_rows($result) < 1) {
		$db->closeDB();
		$toPrint .= "<li>n/a</li></ul></div>";
	} else {
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$onclick = "setTitle(\\\"<h2>Plant Report</h2>\\\");var s=new Array(\\\"sid=".$line['Species_ID']."\\\");call(\\\"functors/plantReportFunctor.php\\\",s,1,0);";
			$toPrint .= "<li><a href='#' onclick='".$onclick."'>".$line['CommonName']."</a> <i>(".$line['ScientificName'].")</i> - ".number_format($line['Population'])."</li>";
		}
		$toPrint .= "</ul></div>";
		$db->closeDB();
	}
	
	$db->connectDB();
	$query = "call region_AnimalReport(".$rid.",".$uid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (region_AnimalReport)');");
	}
	$toPrint .= "<div><h3>Animals</h3><ul>";
	if (mysqli_num_rows($result) < 1) {
		$db->closeDB();
		$toPrint .= "<li>n/a</li></ul></div>";
	} else {
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$onclick = "setTitle(\\\"<h2>Animal Report</h2>\\\");var s=new Array(\\\"sid=".$line['Species_ID']."\\\");call(\\\"functors/animalReportFunctor.php\\\",s,1,0);";
			$toPrint .= "<li><a href='#' onclick='".$onclick."'>".$line['CommonName']."</a> <i>(".$line['ScientificName'].")</i> - ".number_format($line['Population'])."</li>";
		}
		$toPrint .= "</ul></div>";
		$db->closeDB();
	}
	
	$db->connectDB();
	$query = "call region_EnvironmentsReport(".$rid.",".$uid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (region_EnvironmentsReport)');");
	}
	$toPrint .= "<div><h3>Environment Events</h3><ul>";
	if (mysqli_num_rows($result) < 1) {
		$db->closeDB();
		$toPrint .= "<li>n/a</li></ul></div>";
	} else {
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$duration = $line['Duration'];
			if ($duration > 10000) {
				$duration = "&infin;";
			}
			$onclick = "setTitle(\\\"<h2>Environment Report</h2>\\\");var s=new Array(\\\"envid=".$line['Environment_ID']."\\\");call(\\\"functors/eventReportFunctor.php\\\",s,1,0);";
			$toPrint .= "<li><a href='#' onclick='".$onclick."'>".$line['Name']."</a> - ".$duration." years</li>";
		}
		$toPrint .= "</ul></div>";
		$db->closeDB();
	}
	
	$db->connectDB();
	$query = "call region_EnvironmentEffectsReport(".$rid.",".$uid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (region_EnvironmentEffectsReport)');");
	}
	$toPrint .= "<div><h3>Total Environmental Effects</h3><ul>";
	if (mysqli_num_rows($result) < 1) {
		$db->closeDB();
		$toPrint .= "<li>n/a</li></ul></div>";
	} else {
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$toPrint .= "<li>Yearly Rain: ".$line['Precipitation']." mm/yr</li>";
			$toPrint .= "<li>Avg. Heat: ".$line['Heat']."&deg; F</li>";
			$toPrint .= "<li>Yearly Sunlight: ".$line['Sunlight']." hrs/yr</li>";
		}
		$toPrint .= "</ul></div>";
		$db->closeDB();
	}
	echo 'displayReport("'.$toPrint.'");';
}

if( !isset($_POST['rid']) || $_POST['rid'] == '') {
	die("alert('No region was selected. Please try again.');");
}
$rid = $_POST['rid'];
regionReportFunctor($rid);
?>